package cn.stylefeng.guns.modular.rest;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.format.FastDateFormat;
import cn.stylefeng.guns.comm.util.DateUtils;
import cn.stylefeng.guns.modular.school.entity.ParkingInfo;
import cn.stylefeng.guns.modular.school.entity.ParkingPlaceInfo;
import cn.stylefeng.guns.modular.school.entity.ParkingPlaceUseInfo;
import cn.stylefeng.guns.modular.school.model.params.ParkingInfoParam;
import cn.stylefeng.guns.modular.school.model.result.ParkingInfoResult;
import cn.stylefeng.guns.modular.school.service.ParkingInfoService;
import cn.stylefeng.guns.modular.school.service.ParkingPlaceInfoService;
import cn.stylefeng.guns.modular.school.service.ParkingPlaceUseInfoService;
import cn.stylefeng.guns.modular.wordstudy.entity.Plan;
import cn.stylefeng.guns.modular.wordstudy.entity.SignHistory;
import cn.stylefeng.guns.modular.wordstudy.entity.StudyHistory;
import cn.stylefeng.guns.modular.wordstudy.entity.Word;
import cn.stylefeng.guns.modular.wordstudy.mapper.StudyHistoryMapper;
import cn.stylefeng.guns.modular.wordstudy.model.params.PlanParam;
import cn.stylefeng.guns.modular.wordstudy.model.params.StudyHistoryParam;
import cn.stylefeng.guns.modular.wordstudy.model.result.StudyHistoryResult;
import cn.stylefeng.guns.modular.wordstudy.service.PlanService;
import cn.stylefeng.guns.modular.wordstudy.service.SignHistoryService;
import cn.stylefeng.guns.modular.wordstudy.service.StudyHistoryService;
import cn.stylefeng.guns.modular.wordstudy.service.WordService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;


/**
 * 绑定控制器
 *
 * @author
 * @Date 2019-11-18 13:24:11
 */
@RestController
@RequestMapping("/gunsApi/plan")
public class ApiNeedTokenPlanController extends BaseController {

    @Autowired
    private PlanService planService;
    @Autowired
    private StudyHistoryService studyHistoryService;
    @Autowired
    private StudyHistoryMapper studyHistoryMapper;
    @Autowired
    private SignHistoryService signHistoryService;

    @RequestMapping(value = "detail")
    public Object detail(String openId, String dictValue) {
        // 剩余时间
        int remainder = 0;
        Date beginDate;
        Map map = new HashMap();

        Plan plan = planService.getOne(new QueryWrapper<Plan>().eq("open_id", openId));
        if (plan != null) {
            Date updateTime = plan.getUpdateTime();
            if (updateTime == null) {
                beginDate = plan.getCreateTime();
            } else {
                beginDate = updateTime;
            }
            Integer day = plan.getDay();
            Calendar cal = Calendar.getInstance();
            cal.setTime(beginDate);
            cal.add(Calendar.DATE, day);
            remainder = DateUtils.distanceDay(cal.getTime(), DateUtils.getSystemDate());

            //已学单词
            int studiedCnt = this.studyHistoryService.count(new QueryWrapper<StudyHistory>().eq("open_id", openId).eq("review_flag", "2"));
            // 今日剩余
            StudyHistoryParam param = new StudyHistoryParam();
            param.setOpenId(openId);
            StudyHistoryResult studyHistoryResult = studyHistoryService.studyData(param);

            // 本书已经学习
            if (StringUtils.isNoneEmpty(dictValue)) {
//                List<Word> words = wordService.list(new QueryWrapper<Word>().eq("type", dictValue));
//                List<Integer> ids = new ArrayList<>();
//                words.forEach(word -> {
//                    ids.add(word.getId());
//                });
                StudyHistoryParam studyHistoryParam = new StudyHistoryParam();
                studyHistoryParam.setOpenId(openId);
                studyHistoryParam.setReviewFlag("2");
                studyHistoryParam.setWordType(dictValue);
                int count = studyHistoryMapper.customList(studyHistoryParam).size();
                map.put("bookStudiedCnt", count);
            }

            map.put("plan", plan);
            map.put("lastCnt", plan.getWordCnt() - studyHistoryResult.getNewWord() < 0 ? 0 : plan.getWordCnt() - studyHistoryResult.getNewWord());
            map.put("remainder", remainder);
            map.put("studiedCnt", studiedCnt);
        } else {
            map.put("plan", new Plan());
        }
        String today = DateUtils.formateYYYYMMDD(DateUtils.getSystemDate());
        SignHistory signHistory = signHistoryService.getOne(new QueryWrapper<SignHistory>().eq("open_id", openId).eq("sign_date", today));
        if (signHistory != null) {
            map.put("sign", true);
        } else {
            map.put("sign", false);
        }

        // 打卡天数
        int signCnt = signHistoryService.count(new QueryWrapper<SignHistory>().eq("open_id", openId));
        map.put("signCnt", signCnt);


        SUCCESS_TIP.setData(map);
        return SUCCESS_TIP;
    }

    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(PlanParam planParam) {
        this.planService.update(planParam);
        return ResponseData.success();
    }

    @PostMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(PlanParam planParam) {

        Plan plan = this.planService.getOne(new QueryWrapper<Plan>().eq("open_id", planParam.getOpenId()));
        if (plan == null) {
            Plan entity = new Plan();
            ToolUtil.copyProperties(planParam, entity);
            this.planService.save(entity);
        } else {
            plan.setDay(planParam.getDay());
            plan.setWordCnt(planParam.getWordCnt());
            this.planService.saveOrUpdate(plan);
        }


        return ResponseData.success();
    }
}


